package com.wisdomstar.web.controller.wisdomstar;

import com.github.pagehelper.PageInfo;
import com.wisdomstar.common.core.domain.AjaxResult;
import com.wisdomstar.common.core.domain.R;
import com.wisdomstar.common.exception.base.BaseException;
import com.wisdomstar.wordfilter.domain.Word;
import com.wisdomstar.wordfilter.domain.vo.WordVO;
import com.wisdomstar.wordfilter.filter.WordContext;
import com.wisdomstar.wordfilter.filter.WordFilter;
import com.wisdomstar.wordfilter.service.WordService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author RinMi
 * @since 2023/6/10
 */
@RestController
@RequestMapping("ws/word")
@Tag(name = "ws-word-controller", description = "过滤词管理接口")
public class WsWordController {

    @Resource
    private WordService wordService;

    @Resource
    private WordFilter wordFilter;

    @ApiOperation(value = "分页查询过滤词信息", notes = "分页查询过滤词信息", httpMethod = "GET")
    @GetMapping("listWord")
    public AjaxResult listWord(@ApiParam("页码") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                               @ApiParam("页大小") @RequestParam(value = "pageSize", defaultValue = "1") Integer pageSize,
                               @ApiParam("过滤词内容") @RequestParam(value = "wordContent", required = false) String wordContent,
                               @ApiParam("过滤词类型") @RequestParam(value = "wordType", required = false) String wordType) {
        try {
            Word word = new Word();
            word.setWordContent(wordContent);
            word.setWordType(wordType);
            PageInfo<Word> wordPageInfo = wordService.listWord(word, pageNum, pageSize);
            return AjaxResult.success(wordPageInfo);
        } catch (BaseException e) {
            return AjaxResult.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询失败");
        }
    }

    @ApiOperation(value = "新增过滤词信息", notes = "新增过滤词信息", httpMethod = "POST")
    @PostMapping("insertWord")
    public AjaxResult insertWord(@ApiParam("过滤词信息") @RequestBody @Validated WordVO wordVO) {
        try {
            Word word = new Word();
            BeanUtils.copyProperties(wordVO, word);
            int count = wordService.insertWord(word);
            if (count == 1) {
                wordFilter.updateWordContext();
                return AjaxResult.success("新增成功");
            } else {
                return AjaxResult.error("新增失败");
            }
        } catch (BaseException e) {
            return AjaxResult.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("新增失败");
        }
    }

    @ApiOperation(value = "修改过滤词信息", notes = "修改过滤词信息", httpMethod = "POST")
    @PostMapping("updateWord")
    public AjaxResult updateWord(@ApiParam("过滤词信息") @RequestBody WordVO wordVO) {
        try {
            Word word = new Word();
            BeanUtils.copyProperties(wordVO, word);
            int count = wordService.updateWord(word);
            if (count == 1) {
                wordFilter.updateWordContext();
                return AjaxResult.success("修改成功");
            } else {
                return AjaxResult.error("修改失败");
            }
        } catch (BaseException e) {
            return AjaxResult.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改失败");
        }
    }

    @ApiOperation(value = "删除过滤词信息", notes = "删除过滤词信息", httpMethod = "DELETE")
    @DeleteMapping("removeWord")
    public AjaxResult removeWord(@ApiParam("过滤词id") @RequestParam Long wordId) {
        try {
            int count = wordService.removeWord(wordId);
            if (count == 1) {
                wordFilter.updateWordContext();
                return AjaxResult.success("删除成功");
            } else {
                return AjaxResult.error("删除失败");
            }
        } catch (BaseException e) {
            return AjaxResult.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("删除失败");
        }
    }

    @ApiOperation("替换词语")
    @PostMapping("/replaceWord")
    public R<String> replaceWord(String wordContent){
        String replaceContent = wordFilter.replace(wordContent);
        return R.ok(replaceContent);
    }

}
